
<!--
	作者：lsd
	时间：2020-4-15
	描述：物料组排除管理
-->
<template>
	<div class="m-view-materialsExclude m-window">
    	<!-- 搜索头 -->
		<div class="m-condition">
			<el-row :gutter="5">
				<el-form
					:model="formSearch"
					ref="formSearch"
					label-width="70px"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="6">
						<el-form-item label="编码" class="rightItem">
							<el-input
								value-key="id"
								v-model.trim="formSearch.matgroupNo"
								placeholder="请输入"
								clearable
								maxlength="20"
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="名称" class="rightItem">
							<el-input
								value-key="id"
								v-model.trim="formSearch.matgroupName"
								placeholder="请输入"
								clearable
								maxlength="20"
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="4">
						<el-checkbox @change="changeState" v-model="formSearch.state">显示停用</el-checkbox>
					</el-col>
				</el-form>

				<el-col :span="2" :offset="2">
					<el-button type="primary" @click="search(true)">查询</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="openMaterialsDia">新增</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" v-show="!formSearch.state" @click="deleteMaterials">停用</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" v-show="formSearch.state" @click="deleteMaterials">启用</el-button>
				</el-col>
			</el-row>
		</div>

    <!-- 表单 -->
		<div class="m-agTable m-bottom" v-loading="loading">
			<agGridComponents
				ref="agGridComponent"
				:getGridOptions="gridOptions"
				:getColumnDefs="columnDefs"
				:getRowSelection="'multiple'"
				:isColFit="true"
			></agGridComponents>
		</div>

    <!-- 页脚 -->
		<div class="m-page">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="pageInfo.currentPage"
				:page-sizes="[30,50,100,150,200 ]"
				:page-size="pageInfo.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>


    <!-- 新增窗口 -->
		<el-dialog
			title="新增物料组"
			:visible.sync="diaMaterialsExclude"
			width="60"
			v-dialogDrag
			:before-close="closeDia"
			:close-on-click-modal="false"
			:close-on-press-escape="false">

			<el-form
				:model="formMaterialsExclude"
				v-loading="loadingDia"
				ref="formMaterialsExclude"
				label-width="100px"
				class="demo-ruleForm"
				:rules="rules">

				<dialog-srm-materials
					:dialogVisible="diaSelectMaterials"
					@dialogClose="closeMaterialsDia"
					@setSelectData="setSelectData"
					v-if="diaSelectMaterials">
				</dialog-srm-materials>

				<el-form-item label="物料组" prop="matgroup">
					<el-select 
						v-model="formMaterialsExclude.matgroup" 
						value-key="matgroupName" 
						@focus="diaSelectMaterials=true" 
						filterable 
						placeholder="请选择物料组" 
						popper-class="m-view-not-show-option">
						<el-option 
							v-for="item in optionsMaterials"
							:key="item.matgroupNo"
							:label="item.matgroupName"
							:value="item"
						></el-option>
					</el-select>
				</el-form-item>
			</el-form>

			<span slot="footer" class="dialog-footer">
				<el-button @click="closeDia">取消</el-button>
        <!-- 新增物料排除 -->
				<el-button type="primary" @click="addMaterialsExcluse(false)" :loading="loadingDia">保存</el-button>
				<el-button type="primary" @click="addMaterialsExcluse(true)" :loading="loadingDia">保存并继续</el-button>
			</span>
		</el-dialog>

  </div>
</template>
  
<script>
import agGridComponents from "@/components/agGridComponents.js";
import _Api from "@/api";
import localeText from "@/i18n/agGridText";
import dialogSrmMaterials from "./common/dialogSrmMaterials"

/**
 * @group materialsExclude
 * 物料组排除管理
 */
export default {
	name: 'materialsExclude',
	components:{
		agGridComponents, dialogSrmMaterials
	},
	data(){
		return {
			formSearch:{//查询表
				matgroupNo: '',
				matgroupName: '',
				state: 0,
			},
			formMaterialsExclude:{ //新增表
				cond: '等于',
				matgroup: ''
			},
			optionsCondition: [
				{name: '包含', value: 0},{name: '排除', value: 1},{name: '大于', value: 2},
				{name: '大于等于', value: 3},{name: '等于', value: 4},{name: '小于', value: 5},
				{name: '小于等于', value: 6}],
			optionsCompanyName: [],
			optionsMaterials: [],//物料列表
			rowData: [], // 表格数据
			//表格参数
			gridOptions: '',
			columnDefs: [],//非删除表格
			columnDefsDelete: [],//被删除表格
			//页脚参数
			pageInfo: {
				pageSize: 30,
				currentPage: 1,
			},
			total: 0,
			//弹窗参数
			diaMaterialsExclude: false,//新增&编辑采购员弹框
			diaTittle: '',
			diaSelectMaterials: false,//选择物料组弹框开关
			//加载参数
			loading: false,//页面的
			loadingDia: false,//弹框的
			rules: {
				matgroup: [{
					required: true,
					trigger: ['blur', 'change'],
					message: "不能为空",
				}]
			}
		}
	},
	beforeMount () {
		this.gridOptions = {
			context: {
				componentParent: this
			},
			enableColResize: true,
			localeText: localeText,
			suppressRowClickSelection: true,//单元格选中,false为选中整行
		};
		this.columnDefs = [
			{	headerName: "编码", field: "matgroupNo", width: 200,
				checkboxSelection: true,
				headerCheckboxSelection: true			},
			{ headerName: "名称", field: "matgroupName", width: 200 },
			{ headerName: "创建人", field: "creator", width: 100 },
			{ headerName: "创建时间", field: "createDate", width: 150 },
			{ headerName: "修改人", field: "modifyer", width: 100 },
			{ headerName: "修改时间", field: "modifyDate", width: 150 },
		];
		let list = [
				{ headerName: "修改人", field: "modifyer", width: 100 },
				{ headerName: "修改时间", field: "modifyDate", width: 150 },
			]
		this.columnDefsDelete = this.columnDefs.slice(0, 2).concat(list);
	},
	mounted(){
		this.search()
	},
	methods:{
		/**
		 * @vuese
		 * 查询方法
		 * 
		 * @arg 参数是布尔值， true就是点击触发，false就是非点击触发
		 */
		search(isClick){
			this.loading = true;
			this.pageInfo.currentPage = isClick ? 1 : this.pageInfo.currentPage;
			let param = {
				...this.formSearch,
				...this.pageInfo,
			};
			param.state = this.formSearch.state ? 1 : 0;
			_Api
				.searchMaterialsExcluse(param)
				.then(res => {
					this.loading = false;
					if (res.data.code == 1) {
						this.rowData = res.data.rows;
						this.total = res.data.total;
						this.$refs.agGridComponent.gridOptions.api.setRowData(this.rowData);
					} else {
						this.$alert(res.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
					}
				})
				.catch(err => {
					this.loading = false;
					this.$alert(err.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
				});
		},
		
		/**
		 * @vuese
		 * 新增物料方法
		 * 
		 * @arg 参数是布尔值， true就是保存并继续，false保存
		 */
		addMaterialsExcluse(isContinue){
			this.$refs['formMaterialsExclude'].validate((valid) => {
				if (valid) {
					this.loadingDia = true;
					let {creatorId, creator, cond} = this.formMaterialsExclude;
					let {matgroupNo, matgroupName} = this.formMaterialsExclude.matgroup;
					let param = {matgroupNo, matgroupName, creatorId, creator, cond:"等于"}
					_Api
						.addMaterialsExcluse(param)
						.then(res => {
							this.loadingDia = false;
							if (res.data.code == 1) {
								this.$message({ message: "新增成功", type: "success" });
								this.clearParamter();//清空表单
								this.search()//刷新数据
								if (!isContinue) {//保存并继续
									this.closeDia();
								}
							} else {
								this.$alert(res.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
							}
						})
						.catch(err => {
							this.loadingDia = false;
							this.$alert(err.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
						});
				}
			});
		},

		/**
		 * @vuese
		 * 停启用物料方法
		 */
		deleteMaterials(){
			let row = this.$refs.agGridComponent.gridOptions.api.getSelectedRows();
			if(row.length <= 0){
				this.$alert("提示：请至少选择一条数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"					
				});
				return
			}

			let param = {
				ids: row.map(v=>v.id),
				modifyerId: this.$global.user.id,
				modifyer: this.$global.user.employeeName//修改人
			}
			this.$confirm(`是否${this.formSearch.state?'启用':'停用'}这些数据`, "消息提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				distinguishCancelAndClose: true
			}).then(() => {
				this.loading = true;
				_Api
					.deleteMaterialsExcluse(param)
					.then(res => {
						this.loading = false;
						if (res.data.code == 1) {
							this.$message({
								message: `${this.formSearch.state?'启用':'停用'}成功！`,
								type: "success"
							});
							this.search();//刷新
						} else {
							this.$alert(res.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
						}
					})
					.catch(err => {
						this.loading = false;
						this.$alert(err.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
					});
			})
		},

		/**
		 * @vuese
		 * 打开新增物料窗口
		 */
		openMaterialsDia(){
			this.formMaterialsExclude.creatorId = this.$global.user.id;
			this.formMaterialsExclude.creator = this.$global.user.employeeName//创建人
			this.diaMaterialsExclude = true;
		},
		

		/**
		 * @vuese
		 * 是否显示删除数据方法
		 * 
		 * @arg 参数是布尔值，代表是勾中checkbook
		 */
		changeState(val){
			this.$refs.agGridComponent.gridOptions.api.setColumnDefs(val?this.columnDefsDelete:this.columnDefs);
			this.search(true);
		},
		
		/**
		 * @vuese
		 * 改变页脚总条数方法
		 * 
		 * @arg 参数是 int 值，代表一页有多少条数据
		 */
		handleSizeChange (val) {
			this.pageInfo.pageSize = val;
			this.pageInfo.currentPage = 1;
			this.search();
		},

		/**
		 * @vuese
		 * 翻页方法
		 * 
		 * @arg 参数是 int 值，代表页数
		 */
		handleCurrentChange (val) {
			this.pageInfo.currentPage = val;
			this.search();
		},
		
		/**
		 * @vuese
		 * 清空参数方法
		 */
		clearParamter(){
			this.formMaterialsExclude = { 
				cond: '包含',
				matgroup: ''
			}
			this.$refs.formMaterialsExclude.resetFields();
		},

		/**
		 * @vuese
		 * 关闭弹框方法
		 */
		closeDia() {
			this.diaMaterialsExclude = false;
			this.clearParamter();
		},

		/**
		 * @vuese
		 * 弹框选择数据方法
		 * 
		 * @arg 参数是对象，代表选择物料弹框返回的对象
		 */
		setSelectData(val){
			this.optionsMaterials.push(val)
			this.formMaterialsExclude.matgroup = val
		},

		/**
		 * @vuese
		 * 关闭选择物料弹窗
		 */
		closeMaterialsDia(){
			this.diaSelectMaterials = false
		},

		/**
		 * @vuese
		 * 显示选择物料弹框
		 */
		showMaterialsDia(){
			this.diaSelectMaterials = true;
		}
	}
}
</script>